package com.yinhai.message.implement.dwr.sender;

import com.yinhai.message.api.bean.TMMessage;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.sender.IMessageSender;
import com.yinhai.message.core.util.DateFormatUtil;
import com.yinhai.message.implement.dwr.util.RedisTool;
import com.yinhai.sysframework.exception.AppException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanglq
 *         2018.3.22
 */
@Service
public class CacheMessageSender implements IMessageSender<TMMessageVo>{

    @Override
    public List<TMUser> sendMessage(TMMessageVo messageSendVO) {

        TMMessage tmmsg = messageSendVO.getMessage();
        TMUser sendUser = messageSendVO.getUser();

        Map<String, String> msg = new HashMap<>();
        msg.put("mgid", tmmsg.getMgid());
        msg.put("title", tmmsg.getTitle());
        msg.put("content", tmmsg.getContent());
        msg.put("mgdateShow", DateFormatUtil.df2.format(tmmsg.getCreateDate()));
        msg.put("suserid", sendUser.getUserId());
        msg.put("sname", sendUser.getName());
        msg.put("read", "0");
        msg.put("selfType", tmmsg.getSelfType());
		msg.put("otherData1", tmmsg.getOtherData1());
		msg.put("otherData2", tmmsg.getOtherData2());
		msg.put("otherData3", tmmsg.getOtherData3());

        RedisTool redisTool =null;
        try {
            redisTool = RedisTool.getInstance();
        }catch(Exception e){
            throw new AppException("redis异常"+e.getMessage());
        }
		for(TMUser user:messageSendVO.getUserlist() ){
            redisTool.setMessage(tmmsg.getMgid(),user.getAppId(),user.getUserId(),msg);
        }

        return messageSendVO.getUserlist();
    }

}
